package st.business.tepth.business.user.busnumber.workholder.maintain.work.content.standardwork.workdetail;

import java.util.List;

import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.MaintainWorkDetailBean;
import st.business.tepth.business.bean.PartLocationBean;
import st.business.tepth.business.bean.PickGoods;
import st.business.tepth.business.bean.PostDiagnoseWrapperBean;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-04-15 15:34
 */

public class WorkDetailPresenter extends WorkDetailContract.Presenter {
    private final WorkDetailContract.View mView;
    private final WorkDetailContract.Model mModel;

    public WorkDetailPresenter(WorkDetailContract.View view) {
        mView = view;
        mModel = new WorkDetailModel();
    }

    @Override
    void getDetailData(String token, int maintenanceId) {
        addDisposable(mModel.getDetailData(token, maintenanceId,
                new OnDataConvertListener<MaintainWorkDetailBean>() {
                    @Override
                    public void convert(MaintainWorkDetailBean workBean) {
                        mView.showData(workBean);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void confirmDiagnoseList(String token, int repairId, int repairTaskId, int maintainId, String faultDiagnosticList) {
        addDisposable(mModel.postDiagnoseList(token, repairId, repairTaskId, maintainId,
                faultDiagnosticList,
                new OnDataConvertListener<PostDiagnoseWrapperBean>() {
                    @Override
                    public void convert(PostDiagnoseWrapperBean workBean) {
                        if (workBean != null) {
                            mView.postDiagnoseList(workBean.getRepairDiagnosticList());
                        } else {
                            mView.showError("数据错误");
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void deleteDiagnose(String token, int repairId, int faultDiagnosticId, final int position) {
        addDisposable(mModel.deleteDiagnose(token, repairId, faultDiagnosticId,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String workBean) {
                        mView.deleteDiagnose(position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void deleteProgram(String token, int repairId, int faultDiagnosticId, int planId, final int position) {
        addDisposable(mModel.deleteProgram(token, repairId, faultDiagnosticId, planId, new OnDataConvertListener<String>() {
            @Override
            public void convert(String s) {
                mView.deleteProgram(position);
            }

            @Override
            public void error(int code, String msg) {
                mView.showError(msg);
            }
        }));
    }

    @Override
    void pickGoods(String token, int maintenanceId, String materialApplyList) {
        addDisposable(mModel.pickGoods(token, maintenanceId, materialApplyList,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.pickGoodsSucceed();
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }, new OnDataConvertListener<MaintainWorkDetailBean>() {

                    @Override
                    public void convert(MaintainWorkDetailBean workBean) {
                        mView.showData(workBean);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void confirmProgram(String token, int repairId, int repairTaskId, int maintainId, String taskItemList) {
        addDisposable(mModel.postProgram(token, repairId,
                repairTaskId, maintainId, taskItemList, new OnDataConvertListener<List<PickGoods>>() {
                    @Override
                    public void convert(List<PickGoods> programConfirmPickGoods) {
                        mView.showPickGoods(programConfirmPickGoods);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                },
                new OnDataConvertListener<MaintainWorkDetailBean>() {
                    @Override
                    public void convert(MaintainWorkDetailBean workBean) {
                        mView.showData(workBean);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void returnGoods(String token, String items, int planId,
                     final int position) {
        addDisposable(mModel.returnGoods(token, items, planId,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.returnGoodsSucceed(position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void commitMeasureValue(String token, int maintainId, String value) {
        addDisposable(mModel.commitMeasureValue(token, maintainId, value,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.commitValueSucceed();
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void addTempLocation(String token, final int position,
                         final PartLocationBean partLocationBean, String tempLocation) {
        addDisposable(mModel.addTempLocation(token, tempLocation,
                new OnDataConvertListener<List<PartLocationBean>>() {
                    @Override
                    public void convert(List<PartLocationBean> locationBeans) {
                        PartLocationBean partLocationBeanNew = partLocationBean;
                        if (partLocationBean == null) {
                            partLocationBeanNew = new PartLocationBean();
                        }
                        join(locationBeans, partLocationBeanNew);
                        mView.showTempLocation(partLocationBeanNew, position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    private void join(List<PartLocationBean> list, PartLocationBean newP) {
        if (list != null) {
            String name = newP.getPositionName() == null ? "" : newP.getPositionName()+",";
            String code = newP.getPositionCode() == null ? "" : newP.getPositionCode()+",";

            for (PartLocationBean partLocationBean : list) {
                name += partLocationBean.getPositionName() + ",";
                code += partLocationBean.getPositionCode() + ",";
            }
            name = name.substring(0, name.length() - 1);
            code = code.substring(0, code.length() - 1);
            newP.setPositionName(name);
            newP.setPositionCode(code);
        }
    }
}
